<template>
  <div>
    <!-- 索赔结算单查询-详情页面 -->
    <BreakSpace label="结算单信息" :containerStyle="{ marginBottom: '10px' }" />
    <FormPanel ref="formPanel" :list="formList" :cols="3" :formType="'add'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <div style="padding-top: 3rem;"></div>
    <BreakSpace label="结算信息" :containerStyle="{ marginBottom: '10px' }" />
    <FilterTable
      ref="table2"
      :columns="columns2"
      :isToperInfo = false
      :params="fetchParams"
      :dataSource="dataList"
      :datakey="'data'"
      :onColumnsChange="columns2 => (this.columns2 = columns2)"
      :onRowSelectChange="selectChange"
      :isPagination = false
      :isSelectColumn="false"
      height="auto"
      :is-select-column= false
    >
    </FilterTable>
    <div v-show="this.rowData.finalStatementType == '50021001'">
    <FilterTable
      ref="table3"
      :isToperInfo = false
      :columns="columns3"
      :params="fetchParams"
      :dataSource="dataList"
      :datakey="'items'"
      :onColumnsChange="columns3 => (this.columns3 = columns3)"
      :onRowSelectChange="selectChange"
      :isPagination = false
      :isSelectColumn="false"
      height="auto"
      :is-select-column= false
    >
    </FilterTable>
    </div>
    <BreakSpace label="索赔单列表" :containerStyle="{ marginBottom: '10px' }" />
    <FilterTable
      ref="table"
      :dataSource="dataList"
      :columns="columns"
      :params="fetchParams"
      :datakey="'data'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :isPagination = false
      :isSelectColumn="false"
      height="auto"
      :is-select-column= false
    >
    </FilterTable>
        <BreakSpace label="抵扣单列表" :containerStyle="{ marginBottom: '10px' }" />
    <FilterTable
      ref="withholdTable"
      :dataSource="dataList"
      :columns="withholdColumns"
      :params="fetchParams"
      :datakey="'data'"
      :onColumnsChange="columns => (this.withholdColumns = withholdColumns)"
      :onRowSelectChange="selectChange"
      :isPagination = false
      :isSelectColumn="false"
      height="auto"
      :is-select-column= false
    >
    </FilterTable>
    <!-- <div style="padding-top: 3rem;"></div>
    <BaseDialog :visible.sync="suopeidanFlag" :title="'索赔单选择'" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <Selectsuopeidan @close="suopeidanclose" @save="suopeidansave"></Selectsuopeidan>
    </BaseDialog> -->

      <!-- <AjaxButton size="small" type="primary" icon="el-icon-download" :click="ownerExportData">打印结算通知单</AjaxButton> -->
      <multiuse-button @click="closeDialog">关 闭</multiuse-button>
    <!-- <BreakSpace label="考核单列表" :containerStyle="{ marginBottom: '10px' }" />
    <FilterTable
      ref="table1"
      :columns="columns1"
      :params="fetchParams"
      :datakey="'items'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :isPagination = false
      :isSelectColumn="false"
      height="auto"
      :is-select-column= false
    >
    </FilterTable>
    <div style="padding-top: 3rem;"></div>
    <BaseDialog :visible.sync="kaohedanFlag" :title="'考核单选择'" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <SelectKaoHedan @close="kaohedanclose" @save="kaohedansave"></SelectKaoHedan>
    </BaseDialog>
    <div style="padding-top: 3rem;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <multiuse-button @click="closeDialog">关 闭</multiuse-button>
    </div>
    <BaseDialog :title="'查看附件'" :visible.sync="upVisible" destroyOnClose>
      <uploadFile :rowData="assessId" @close="upVisible = false"></uploadFile>
    </BaseDialog> -->
  </div>
</template>
<script>

  import { isFormEmpty } from '@/utils/index';
  import { getCloseAccountCount, getPartType,getClaimInfoByNo,getCostCountByNo,editSave,quyrpBatch,updateBusinessMoney,getCbClaim } from '@/api/claim/factory/ClaimStatementQueryFactory';
  // import { approve,quyrpBatch} from '@/api/claim/factory/ClaimInvoiceCheck';
  import { dictionary } from '@/utils/dictMixin';
  import uploadFile from "@/pages/claimDemage/BasicData/ClaimInvoiceCheck/uploadFile";
  export default {
    mixins: [dictionary],
    props: ['rowData'],
    components: {
      uploadFile
    },
    data() {
      return {
        dataList: [],
        dataList1: [],
        kaohesum: 0,
        suopeisum: 0,
        huanxiajiandataList: [],
        fujiaxiangmudataList: [],
        visible: false,
        //索赔单列表开关
        suopeidanFlag: false,
        kaohedanFlag: false,
        visibleDrawer: false,
        collapse: false,
        infoId: null,
        flterValue: {}, //参数
        columns: this.creatHeader(),
        withholdColumns:this.creatWithholdHeader(),
        // columns1: this.creatHeader1(),  考核单
        columns2:this.creatHeader2(),
        columns3:this.creatHeader3(),
        fetchParams: {}, //ipa 接口
        selectedRows: [], //选中服务中心
        formList: this.createFormList(),
        editRow: {},
        saveData: null,
        upVisible:false,
        assessId:null
      };
    },
    async created() {
      // await this.chuqi1();
      //await this.chuqi();
      //await this.getDetailFun(this.rowData.settleId);
      await this.findBuyItems();
      await this.valueForTable3();
      //await this.valueForTable2();
    },
    methods: {

      sumAmount() {
        let sum = 0;

        //索赔金额
        this.$refs.table.$refs.pageTable.list.map(x => {
          sum += Number(x.sumAmount);
        });
        //考核金额
        this.$refs.table1.$refs.pageTable.list.map(x => {
          sum += Number(x.assessAmount);
        });

        // this.$refs.peijianhuanshangjiantable.$refs.pageTable.list.map(x => {
        //   sum += Number(x.cmsClaimPrice);
        // });

        this.formList.map(x => {
          if('settleAmount' == x.fieldName) {
            x.initialValue = sum;
          }
        });

      },
      //索赔车辆属性
      // async chuqi() {
      //   // const res = await getPartType({ type: 5543 });
      //   // if (res.resultCode == 200) {
      //   //   this.formList.find(x => x.fieldName == 'carType').initialValue = '';
      //   //   this.formList.find(x => x.fieldName == 'carType').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      //   // }
      // },
      formChangeHandle(val) {
        this.saveData = val;
      },
      //搜索获取数据
      changeHandle(val) {
        this.fetchParams = Object.assign({}, this.fetchParams, val);
      },
      //索赔单计算
      suopeidansave(val) {
        this.$refs.table.$refs.pageTable.EXECUTE_INSERT(val);
        let suopeijine = 0;
        this.$refs.table.$refs.pageTable.list.map(x => {
          suopeijine += Number(x.sumAmount);
        });
        this.formList.map(x => {
          if ('claimAmount' == x.fieldName) {
            x.initialValue = Number(suopeijine);
          }
        });
        this.suopeisum=suopeijine;
        this.sumAmount();
        this.suopeidanFlag = false;
        this.fetchParams = { ...this.fetchParams };
      },//考核单计算
      kaohedansave(val) {
        this.$refs.table1.$refs.pageTable.EXECUTE_INSERT(val);

        let kaohejine = 0;
        this.$refs.table1.$refs.pageTable.list.map(x => {
          kaohejine += Number(x.assessAmount);
        });
        this.formList.map(x => {
          if ('assessAmount' == x.fieldName) {
            x.initialValue = Number(kaohejine);
          }
        });
        this.kaohesum=kaohejine;
        this.sumAmount();
        this.kaohedanFlag = false;
        this.fetchParams = { ...this.fetchParams };
      },
      suopeidanclose() {
        this.suopeidanFlag = false;
      },
      // suopeidanadd() {
      //   this.suopeidanFlag = true;
      // },
      kaohedanclose() {
        this.kaohedanFlag = false;
      },
      kaohedanadd() {
        this.kaohedanFlag = true;
      },

      //选中
      selectChange(rows) {
        this.selectedRows = rows;
      },
      //保存
      async saveDialog() {
        this.$refs.formPanel.SUBMIT_FORM();

        let str = '';
        this.$refs.table.$refs.pageTable.list.map(x => {
          str += x.claimId + ',';
        });
        this.saveData.claimIds=str;

        let str1 = '';
        this.$refs.table1.$refs.pageTable.list.map(x => {
          str1 += x.assessId + ',';
        });
        this.saveData.assessIds=str1;

        for (let a in this.editRow) {
          if (this.saveData[a]) {
            this.editRow[a] = this.saveData[a];
          }
        }
        if (this.saveData !== null) {
          let res = await editSave(this.editRow);
          if (res.resultCode === 200) {
            this.closeDialog('refresh');
            return this.$notify.success({ title: '提示信息', message: '保存成功' });
            this.$emit('save');
          } else {
            this.$notify.error({ title: '提示信息', message: res.errMsg || '保存失败!' });
            this.closeDialog();
          }

        }else {
          return this.$notify.warning({ title: '提示信息', message: '有未校验通过的信息' });
        }

      },
      //给input框赋值
      async findBuyItems() {
      this.formList.map(x => {
        x.initialValue = this.rowData[x.fieldName];
      });
       let res = await getClaimInfoByNo(this.rowData.settleNo);
       //为table赋值(索赔单)
        if (200 == res.resultCode) {
          let data = res.data;
          if (isFormEmpty(data)) {
            data = {};
          }
          // let records = data.claimList;
          // if (isFormEmpty(records)) {
          //   records = [];
          // }
          this.$refs.table.$refs.pageTable.EXECUTE_INSERT(data);
          this.$refs.table.$refs.pageTable.list.map((x) => {
            if(x.ifPrice == '是'){
              x.finalSumAmount =x.priceAmount;
            }
          });
        }

        let res2 = await getCloseAccountCount(this.rowData.settleNo);
       //为table赋值(结算信息)
        if (200 == res2.resultCode) {
          var values = res2.data;
          var detail = Object.assign({
            project: "索赔单数量"
          },values[0]);
          if (isFormEmpty(detail)) {
            detail = {};
          }
          this.$refs.table2.$refs.pageTable.EXECUTE_INSERT(detail);
        }
    },
    //为table3赋值  费用明细
   async valueForTable3(){

      let res3 = await getCostCountByNo(this.rowData.settleNo);
       //为table赋值(结算信息)
        if (200 == res3.resultCode) {
          var values = res3.data;
          var detail = Object.assign({
            project: "金额(元)"
          },values[0]);
          if (isFormEmpty(detail)) {
            detail = {};
          }
          this.$refs.table3.$refs.pageTable.EXECUTE_INSERT(detail);
        }

        
        //为抵扣单赋值
        let res4 = await getCbClaim(this.rowData.settleNo);
        if (200 == res4.resultCode) {
          let data = res4.data;
          if (isFormEmpty(data)) {
            data = {};
          }
          this.$refs.withholdTable.$refs.pageTable.EXECUTE_INSERT(data);
        }
    },
      //取消操作
      closeDialog() {
        this.$emit('close', { visible: false, data: this.selectedRows, type: 'close' });
      },
      editFile(row){
        this.upVisible = true;
        this.assessId = row.assessId;
      },
      chooseOrderNo() {
        this.visible = true;
      },
      add() {},
      createFormList() {
        return [
          {
            type: 'INPUT',
            label: '结算单号',
            fieldName: 'settleNo',
            disabled: true // 只读
          },
          {
            type: 'INPUT',
            label: '服务中心代码',
            fieldName: 'dealerCode',
            disabled: true // 只读
          },
          {
            type: 'INPUT',
            label: '服务中心名称',
            fieldName: 'dealerName',
            disabled: true // 只读
          },
          {
            type: 'SELECT',
            label: '结算单类型',
            fieldName: 'finalStatementType',
            disabled: true, // 只读
            itemList: this.createDictList('5002')
          },
          {
            type: 'INPUT',
            label: '结算月份',
            fieldName: 'settleDate',
            disabled: true // 只读
          },
          {
            type: 'INPUT',
            label: '结算金额',
            fieldName: 'settleAmount',
            disabled: true // 只读
          },
        ];
      },
      creatHeader2() {
        return [
          // {
          //   title: '序号',
          //   dataIndex: 'index',
          //   width: 80,
          //   sorter: true,
          //   render: props => {
          //     return <span>{props.row.index + 1}</span>;
          //   }
          // },
          { title: '项目', dataIndex: 'project', sorter: true },
          { title: '正常索赔', dataIndex: 'normalClaim', sorter: true },
          { title: '售前索赔', dataIndex: 'commercialClaim', sorter: true },
          { title: '首保', dataIndex: 'firstMaintenance', sorter: true },
          { title: 'PDI', dataIndex: 'pdi', sorter: true },
          { title: '服务活动', dataIndex: 'serviceAction', sorter: true },
          { title: '技术活动', dataIndex: 'superBaoClaim', sorter: true },
          { title: '备件保修', dataIndex: 'partClaim', sorter: true },
          { title: '精品保修', dataIndex: 'cellClaim', sorter: true },
          { title: '善意索赔', dataIndex: 'specialClaim', sorter: true }
        ];
      },
      creatHeader3() {
        return [
          // {
          //   title: '序号',
          //   dataIndex: 'index',
          //   width: 80,
          //   sorter: true,
          //   render: props => {
          //     return <span>{props.row.index + 1}</span>;
          //   }
          // },
          { title: '项目', dataIndex: 'project', sorter: true },
          { title: '工时费', dataIndex: 'labourAmount', sorter: true },
          { title: '管理费', dataIndex: 'partManageAmount', sorter: true },
          { title: '外出服务费', dataIndex: 'helpAmount', sorter: true },
          { title: '附加费用', dataIndex: 'addAmount', sorter: true },
          { title: '首保费用', dataIndex: 'firstMaintenanceCount', sorter: true },
          { title: 'PDI费用', dataIndex: 'pdiCount', sorter: true },
          { title: '服务活动费用', dataIndex: 'serviceCount', sorter: true },
          { title: '技术活动费用', dataIndex: 'upgradeCount', sorter: true },
          { title: '激励费用', dataIndex: 'businessAmount', sorter: true },
          { title: '善意索赔', dataIndex: 'goodWillClaimCount', sorter: true },
          { title: '旧件回运费用', dataIndex: 'freightAmount', sorter: true } //单独算
        ];
      },
      //抵扣单列表
      creatWithholdHeader() {
        return [
          {
            title: '序号',
            dataIndex: 'index',
            width: 80,
            sorter: true,
            render: props => {
              return <span>{props.row.index + 1}</span>;
            }
          },
          { title: '抵扣单号', dataIndex: 'cbNo', sorter: true },
          { title: 'VIN', dataIndex: 'vin', sorter: true },
          { title: '车型', dataIndex: 'modelName', sorter: true },
          // { title: '抵扣类型', dataIndex: 'cbType', sorter: true,dictItems: this.createDictList('5510') },
          { title: '合计金额', dataIndex: 'cbSumAmount', sorter: true,precision:2 },
          { title: '工时费', dataIndex: 'cbLabourAmount', sorter: true,precision:2 },
          { title: '材料费', dataIndex: 'cbPartAmount', sorter: true,precision:2 },
          { title: '附加费用', dataIndex: 'cbAddAmount', sorter: true,precision:2 },
          { title: '外出费用', dataIndex: 'cbOutAmount', sorter: true,precision:2 },
          { title: '备件管理费', dataIndex: 'cbPtManageAmount', sorter: true,precision:2 },
          { title: '物流费用', dataIndex: 'cbTravelAmount', sorter: true,precision:2 },
          // { title: '紧急回运运费', dataIndex: 'cbJfAmount', sorter: true,precision:2 },
          { title: '其他费用', dataIndex: 'cbOtherAmount', sorter: true,precision:2 },
          { title: this.rowData.finalStatementType == '50021001' ? '服务费剩余费用' : '材料费剩余费用', 
          dataIndex: this.rowData.finalStatementType == '50021001' ? 'serviceResidue' : 'partResidue', sorter: true,precision:2 },
          // { title: '材料费剩余费用', dataIndex: 'partResidue', sorter: true,precision:2 },
          { title: '审核时间', dataIndex: 'confirmedAt', sorter: true },
          { title: '审核人', dataIndex: 'confirmedBy', sorter: true },
        ];
      },
      //索赔单列表
      creatHeader() {
        return [
          {
            title: '序号',
            dataIndex: 'index',
            width: 80,
            sorter: true,
            render: props => {
              return <span>{props.row.index + 1}</span>;
            }
          },
          { title: '索赔单号', dataIndex: 'claimNo', sorter: true },
          { title: 'VIN', dataIndex: 'vin', sorter: true },
          { title: '车型名称', dataIndex: 'modelName', sorter: true },
          { title: '索赔类型', dataIndex: 'cmsClaimType', sorter: true,dictItems: this.createDictList('5510') },
          { title: '索赔金额', dataIndex: 'sumAmount', sorter: true,precision:2 },
          { title: '工时费', dataIndex: 'labourAmount', sorter: true,precision:2 },
          { title: '材料费', dataIndex: 'partAmount', sorter: true,precision:2 },
          { title: '备件管理费', dataIndex: 'partManageAmount', sorter: true ,precision:2}, 
          { title: '附加费用', dataIndex: 'addAmount', sorter: true ,precision:2},
          //  { title: '审核合计费用', dataIndex: 'claimSumAmount', sorter: true,precision:2 }, 
          { title: '外出救援费用', dataIndex: 'helpAmount', sorter: true ,precision:2}, 
          { title: 'PDI', dataIndex: 'priceAmount', sorter: true,precision:2 },  
          { title: '审核时间', dataIndex: 'checkAt', sorter: true },
          { title: '审核人', dataIndex: 'checkBy', sorter: true },
        ];
      },
      //考核单列表
      // creatHeader1() {
      //   return [
      //     {
      //       title: '序号',
      //       dataIndex: 'index',
      //       width: 80,
      //       sorter: true,
      //       render: props => {
      //         return <span>{props.row.index + 1}</span>;
      //       }
      //     },
      //     {
      //       title: '操作',
      //       dataIndex: 'column-action',
      //       width: 90,
      //       render: props => {
      //         return (
      //           <div style="text-align:center">
      //             <multiuse-button type="text" onClick={() => this.editFile(props.row)} >
      //               查看附件
      //             </multiuse-button>
      //           </div>
      //         );
      //       }
      //     },
      //     { title: '考核单号', dataIndex: 'assessNo', sorter: true },
      //     { title: '考核类型', dataIndex: 'assessTypeStr', sorter: true },
      //     { title: '考核金额', dataIndex: 'assessAmount', sorter: true },
      //     { title: '考核原因', dataIndex: 'assessReason', sorter: true },
      //     { title: '考核日期', dataIndex: 'assessDate', sorter: true },
      //     { title: '考核人', dataIndex: 'assessByStr', sorter: true },
      //     { title: '主键', dataIndex: 'assessId', hidden: true }
      //   ];
      // }
    }
  };
</script>
<style lang="less" scoped></style>
